//
//  FLInterfaceManager.m
//  Pods
//
//  Created by LinXiaoBin on 16/3/15.
//
//

#import "FLInterfaceManager.h"
#import "FLInterfaceBase.h"

@interface FLInterfaceManager() <FLInterfaceManagerDelegate> {
    NSLock *_lock;
    NSMutableSet *_interfaces;
}

@end

@implementation FLInterfaceManager

+ (instancetype)sharedInstance
{
    static FLInterfaceManager *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _interfaces = [NSMutableSet set];
        _lock = [[NSLock alloc] init];
    }
    return self;
}

#pragma mark - private

- (void)addInterface:(FLInterfaceBase *)interface
{
    if ([interface isKindOfClass:[FLInterfaceBase class]]) {
        [_lock lock];
        interface.managerDelegate = self;
        [_interfaces addObject:interface];
        [_lock unlock];
    }
}

- (void)removeInterface:(FLInterfaceBase *)interface
{
    if ([interface isKindOfClass:[FLInterfaceBase class]]) {
        [_lock lock];
        [_interfaces removeObject:interface];
        [_lock unlock];
    }
}

#pragma mark FLInterfaceManagerDelegate <NSObject>
- (void)flInterfaceDidRequestFinished:(FLInterfaceBase *)interface
{
    [self removeInterface:interface];
}

@end
